/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.anniversary.domain.CollegeStatisticDetail;
import me.zhengjie.modules.anniversary.repository.CollegeStatisticDetailRepository;
import me.zhengjie.modules.anniversary.service.CollegeStatisticDetailService;
import me.zhengjie.modules.anniversary.service.dto.*;
import me.zhengjie.modules.anniversary.service.mapstruct.CollegeStatisticDetailMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2020-07-27
**/
@Service
@RequiredArgsConstructor
public class CollegeStatisticDetailImpl implements CollegeStatisticDetailService {

    private final CollegeStatisticDetailRepository statisticsInfoRepository;
    private final CollegeStatisticDetailMapper collegeStatisticDetailMapper;


    @Override
    public Map<String,Object> queryAll(CollegeStatisticDetailQueryCriteria criteria, Pageable pageable){
        Page<CollegeStatisticDetail> page = statisticsInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(collegeStatisticDetailMapper::toDto));
    }

    @Override
    public List<CollegeStatisticDetailDto> queryAll(CollegeStatisticDetailQueryCriteria criteria){
        return collegeStatisticDetailMapper.toDto(statisticsInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public void download(List<CollegeStatisticDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CollegeStatisticDetailDto collegeStatisticDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("院系名称", collegeStatisticDetail.getCollege());
            map.put("原院系名称", collegeStatisticDetail.getOldCollegeName());
            map.put("年份", collegeStatisticDetail.getEnterDate());
            map.put("专业", collegeStatisticDetail.getMajor());
            map.put("校友总数", collegeStatisticDetail.getCount());
            map.put("待认证数", collegeStatisticDetail.getUnverifyCount());
            map.put("认证待审核数", collegeStatisticDetail.getVerifingCount());
            map.put("认证通过数", collegeStatisticDetail.getVerifyPassCount());
            map.put("认证未通过数", collegeStatisticDetail.getVerifyUnPassCount());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
